package cn.tanglong.user.service.impl;

import cn.tanglong.basic.constant.PhoneConstant;
import cn.tanglong.basic.exception.MyException;
import cn.tanglong.basic.service.impl.BaseServiceImpl;
import cn.tanglong.basic.util.MD5Utils;
import cn.tanglong.basic.util.StrUtils;
import cn.tanglong.user.constant.UserConstant;
import cn.tanglong.user.domain.Logininfo;
import cn.tanglong.user.domain.User;
import cn.tanglong.user.dto.UserDto;
import cn.tanglong.user.mapper.LogininfoMapper;
import cn.tanglong.user.mapper.UserMapper;
import cn.tanglong.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User>implements IUserService {
    
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void register(UserDto userDto) {
        // 判断非空
        if(StringUtils.isEmpty(userDto.getPhone())
            || StringUtils.isEmpty(userDto.getConfirmPwd())
            || StringUtils.isEmpty(userDto.getPassword())
            || StringUtils.isEmpty(userDto.getConfirmPwd()))
        {
            throw new MyException("你是瞎子？");
        }
        // 判断手机号是否存在 存在就直接提示登录
        User p = userMapper.findByPhone(userDto.getPhone());
        if(p != null){
            throw new MyException("手机号已经注册，你还在注册嘎哈？");
        }

        // 判断密码和确认密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new MyException("两次输入的密码不一致，啥b");
        }
        // 判断验证码是否过期
        Object ro = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX  + userDto.getPhone());
        if(ro == null){
            throw new MyException("已经三分钟了，懒狗");
        }
        // 获取验证码
        String code = ro.toString().split(":")[1];
        // 判断验证码是否一致
        if(!userDto.getVeryfiCode().equalsIgnoreCase(code)){
            throw new MyException("验证码都能输错！憨批！");
        }
        // 调用我们准备的user方法获得user对象
        User user = userDtoToUser(userDto);
        // 调用logininfo方法获得logininfo对象
        Logininfo logininfo = userDtoToLogininfo(user);
        // 先添加logininfo 然后添加user(logininfo的id时user需要的,先加user会缺字段)
        // 判断是员工还是用户
        logininfo.setType(1);
        // 添加logininfo 返回自增主键
        logininfoMapper.add(logininfo);
        // 拿到logininfo_id
        user.setLogininfo_id(logininfo.getId());
        // 添加user表
        userMapper.add(user);
    }

    /* 通过userDto准备user：把所有需要使用的数据全部传入user对象中 */
    private User userDtoToUser(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt =StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }

    private Logininfo userDtoToLogininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        logininfo.setDisable(1);
        return logininfo;
    }
}
